package com.lazily.listlection_;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author:  GZQ
 * @date:  2025/4/17 22:37
 * @description: 练习
 * @version: 1.0
 */

public class CollectionExercise {
	// 程序入口
	public static void main(String[] args) {

		List list = new ArrayList();

		list.add(new Dog("拉布拉多", 12));
		list.add(new Dog("小七", 7));
		list.add(new Dog("阿拉斯加", 2));

		// 迭代器
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Object obj = iterator.next();
			System.out.println("obj: " + obj);
		}

		// 增强for
		for (Object dog : list) {
			System.out.println("dog: " + dog);
		}

	}
}

/**
 * 创建 3 个Dog {name, age} 对象, 放入到 ArrayList 中, 赋给 List 引用
 * 迭代器和增强for循环两种方式来遍历
 * 重写 Dog 的toString 方法, 输出name和 age
 */

class Dog {

	private String name;
	private int age;

	public Dog(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Dog{" +
				"名字：'" + name + '\'' +
				", 年龄: " + age +
				'}';
	}
}